﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml.Linq;
using Excel = Microsoft.Office.Interop.Excel;
using Office = Microsoft.Office.Core;
using Microsoft.Office.Tools.Excel;
using System.Xml;
using System.Collections.ObjectModel;
using Microsoft.Office.Core;
using System.Reflection;
using System.IO;
using System.Runtime.InteropServices;
using Server.Business;
using Server.Database;
using Server.Business.proto;

namespace Server.Business
{
    public class SpecGenerator : AbstractSpecGenerator
    {
        protected override string TemplateName
        {
            get { return "SPEC简表"; }
        }

        protected override string ConvertToXml(ProdDB lighting)
        {
            StringBuilder sb = new StringBuilder();
            using (XmlWriter writer = XmlWriter.Create(sb))
            {
                writer.WriteStartElement("灯具的相关表");
                Write(writer, lighting);
                writer.WriteEndElement();
                writer.Flush();
            }
            return sb.ToString();
        }

        private void Write(XmlWriter xmlWriter, ProdDB data)
        {
            ResourceProvider provider = new ResourceProvider(); 

            xmlWriter.WriteStartElement("基本信息");
            xmlWriter.WriteElementString("产品品牌", data.ProdLabel != null ? data.ProdLabel : string.Empty);

            ResItem clsItem = (from r in provider.LightingType
                               where r.ItemID == data.AdpClass
                               select r).SingleOrDefault();
            xmlWriter.WriteElementString("光源种类", clsItem != null ? clsItem.ItemName : string.Empty);
            xmlWriter.WriteElementString("光源型号", data.AdpVersion != null ? data.AdpVersion : string.Empty);
            xmlWriter.WriteElementString("色温或颜色", data.AdpColor != null ? data.AdpColor : string.Empty);

            xmlWriter.WriteElementString("材质说明", data.PhyMaterial != null ? data.PhyMaterial : string.Empty);
            xmlWriter.WriteElementString("外观颜色", data.PhyColor != null ? data.PhyColor : string.Empty);
            xmlWriter.WriteElementString("外形尺寸", data.PhySize != null ? data.PhySize : string.Empty);
            xmlWriter.WriteElementString("防护等级", data.PhyDefend != null ? data.PhyDefend : string.Empty);

            ResItem impactItem = (from r in provider.ImpactType
                                  where r.ItemID == data.PhyImpact
                                  select r).SingleOrDefault();
            xmlWriter.WriteElementString("防撞击等级", impactItem != null ? impactItem.ItemName : string.Empty);

            ResItem item = (from r in provider.LightingExplain
                            where r.ItemID == data.OptExplain
                            select r).SingleOrDefault();
            xmlWriter.WriteElementString("光束角", data.OptAngle != null ? data.OptAngle : string.Empty);

            ResItem voltageItem = (from r in provider.WorkVoltage
                                   where r.ItemID == data.EleVoltage
                                   select r).SingleOrDefault();
            xmlWriter.WriteElementString("工作电压", voltageItem != null ? voltageItem.ItemName : string.Empty);

            xmlWriter.WriteElementString("电器类型", data.EleType != null ? data.EleType : string.Empty);
            xmlWriter.WriteElementString("光源功率", data.EleLgtPower != 0 ? data.EleLgtPower.ToString() : string.Empty);
            xmlWriter.WriteElementString("整灯功耗", data.EleAllPower != 0 ? data.EleAllPower.ToString() : string.Empty);

            ResItem controlItem = (from r in provider.ControlType
                                   where r.ItemID == data.EleCtlType
                                   select r).SingleOrDefault();
            xmlWriter.WriteElementString("控制方式", controlItem != null ? controlItem.ItemName : string.Empty);

            item = (from r in provider.SetupType
                            where r.ItemID == data.ProjSetup
                            select r).SingleOrDefault();
            xmlWriter.WriteElementString("安装方式", item != null ? item.ItemName : string.Empty);
            xmlWriter.WriteElementString("安装尺寸", data.ProjSize != null ? data.ProjSize : string.Empty);
            xmlWriter.WriteElementString("备注", data.ProjBaikup != null ? data.ProjBaikup : string.Empty);
            xmlWriter.WriteElementString("可选配件", data.ProjParts != null ? data.ProjParts : string.Empty);
            xmlWriter.WriteEndElement();
        }
    }
}
